// pages/my/mall/index.js
const api = require('../../../fetch/api')
const { baseURL } = require('../../../fetch/http')
const app = getApp()
Page({
  /**
   * 页面的初始数据
   */
  data: {
    tabs: [],
    tabIndex: 0,
    toView: 's0',
    goods_classify_id: '',
    arr: [],
    loading: false,
    finished: false,
    page: 1
  },  
  async classify() {
    let res = await api.goodsClassify()
    this.setData({
      tabs: res.data,
      goods_classify_id: res.data[0].id
    })
  },
  detail(e) {
    const { id } = app.dataSet(e)
    wx.navigateTo({
      url: '/pages/my/integralGoods/index?id=' + id,
    })
  },
  switchTab(e) {
    const { index } = app.dataSet(e)
    if (this.data.tabIndex != index) {
      this.setData({
        tabIndex: index,
        goods_classify_id: this.data.tabs[index].id,
        toView: 's' + index
      })
      this.clear()
      this.goods()
    }
  },  
  clear() {
    this.setData({
      page: 1,
      loading: false,
      finished: false,
      arr: []
    })
  },
  async goods() {
    if (!this.data.loading) {
      this.setData({
        loading: true
      })
      let res = await api.goods({
        page: this.data.page,
        goods_type: 2,
        goods_classify_id: this.data.goods_classify_id
      })
      let finished = false
      res.data.length == 0 && (finished = true)
      this.setData({
        arr: [...this.data.arr,...res.data],
        finished,
        loading: false
      })
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  async onLoad (options) {
    await this.classify()
    this.goods()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    let {
      page,
      finished
    } = this.data
    if (!finished) {
      this.setData({
        page: page + 1
      })
      this.goods()
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})